<?php
class SiteController extends FrontController {
    //public $staticAlias='application.views.site.page.';
     /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
            'goldRate'=>array(
                'class' => 'GoldRateAction',
                'view'=>'gold_rate',
            ),
            // captcha action renders the CAPTCHA image displayed on the contact page
            /*'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            'ajaxUpload'=>'ext.EAjaxUpload.EAjaxUploadAction',*/
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            /*'page' => array(
                'class' => 'CViewAction',
                'layout'=>'sign-column2',
            ),*/
        );
    }

    /**
    * Render index with news content and post content (idea content)
    * 
    */
    public function actionIndex()
    {
        //FSM::run('Core.Settings.db2php', array('module'=>''));
        //Yii::app()->layout = '//layouts/column3';
        //$res = FSM::run('Core.Category.getParentIds',array('id'=>10,'include_child_cat'=>1));
        //var_dump($res->ids); die;
        $this->pageTitle = $this->getSiteNameByLanguage().' | '.Yii::t('site/general','Home');
        $this->render('index');
    }
    
    public function actionContact()
    {
        $this->pageTitle = $this->getSiteNameByLanguage().' | '.Yii::t('site/general','Contact');
        $model = new ContactForm();
        if (Yii::app()->request->IsPostRequest && isset($_POST['ContactForm'])) {
            $contactEmail = trim(SiteSettings::CONTACT_EMAIL);
            if ($contactEmail == '')
                throw new CHttpException(500, 'Contact E-mail cannot be blank.');
            $model->setAttributes($_POST['ContactForm'],false);   
            Yii::import('Core.extensions.vendors.mail.YiiMailMessage');
            Yii::app()->mail->viewPath = 'application.views.mail';
            $message = new YiiMailMessage;
            $message->view = 'contact';
            $message->setSubject('[International Environment Company] A contact message from '.$model->email);
            $message->setBody(array(
                'model' => $model,
                ), 'text/html');
            $message->addFrom($model->email, $model->name);
            $message->addTo($contactEmail, 'International Environment Company (IEC)');
            
            if (Yii::app()->mail->send($message)) {
                user()->setFlash('send_contact_success',Yii::t('site/general','Thank you for contacting us'));
                $this->refresh();
            }
        }
        $this->render('contact',array('model'=>$model));
    }
    
    public function actionSearch()
    {
        $keyword = $this->get('keyword','','xss');
        $keyword = trim(str_replace(array('"',"'"),'',$keyword));
        $this->render('search',array('keyword'=>$keyword));    
    }
    
    public function actionTestLang()
    {
        echo 'day chinh la '.Yii::app()->language;
    }
    
    public function actionSearchArticle()
    {
        if (Yii::app()->request->isAjaxRequest)
        {
            $params = array('page_size'=>6);
            if (isset($_POST['keyword']))
                $params['keyword'] = $_POST['keyword'];
            echo $this->widget('Site.components.SearchedArticleListWidget',$params,TRUE);
        }
    }
    
    public function actionSearchProduct()
    {
        if (Yii::app()->request->isAjaxRequest)
        {
            $params = array('page_size'=>6);
            if (isset($_POST['keyword']))
                $params['keyword'] = $_POST['keyword'];
            echo $this->widget('Site.components.SearchedProductListWidget',$params,TRUE);
        }
    }
    
    public function actionSearchDocument()
    {
        if (Yii::app()->request->isAjaxRequest)
        {
            $params = array('page_size'=>6);
            if (isset($_POST['keyword']))
                $params['keyword'] = $_POST['keyword'];
            echo $this->widget('Site.components.SearchedDocumentListWidget',$params,TRUE);
        }
    }

    public function actionTest(){
        $res = FSM::run('application.GoldParser.getVietComBankExchange');
        echo $res->result_string;
    }
    
    public function actionShowMap()
    {
        Yii::app()->layout = '//layouts/popup';
        $this->render('show_map');
    }
}
